/**
 * Created by lixiaodi on 2018/9/27 0027.
 */
mini.parse();
var grid = mini.get("datagrid1");
//分页控件
var pager = grid.getBottomPager();
var editBtn = mini.get('editBtn');
var removeBtn = mini.get('removeBtn');
//grid.load(ajaxUrl + "/modular/list.do");            	//请求列表显示数据
//grid.load();                                        	//显示列表
var url = bootPATH + "../frame/frame1/pages/";      	//路径前缀
var topSelect;
//初始化加载数据
$(function(){
	httpRequest({},1,pager.getPageSize());
});
//选中行事件
function selectEvent(){
	var row = grid.getSelected();
	//判断是是否选中行
	if(row){
		//判断是否系统预制
		if(row.modularIsPreset == '是'){
			editBtn.disable();
			removeBtn.disable();
		}else{
			editBtn.enable();
			removeBtn.enable();
		}
	}else{
		editBtn.enable();
		removeBtn.enable();
	}
}
//表头添加菜单按钮
var menu = new ColumnsMenu(grid);
//增加
function add() {
    mini.open({
        url: url +"alertBox/modularWindow.html",
        title: "新增模块", width: 600, height: 400,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = { action: "new"};
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {
			httpRequest({},0,pager.getPageSize());
        }
    });
}
//编辑
function edit() {
    var row = grid.getSelected();
    if (row) {
        mini.open({
            url: url +"alertBox/modularWindow.html",
            title: "编辑模块", width: 600, height: 400,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "edit", id: row.modularId };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
				httpRequest({},0,pager.getPageSize());
            }
        });
    } else {
        mini.alert("请选中一条记录");
    }
}
//删除
function remove() {
    var rows = grid.getSelecteds();
    if (rows.length > 0) {
        mini.confirm("确定删除选中记录？","系统提示",function(action){
            if (action == "ok") {
                var ids = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.modularId);
                }
                var id = ids.join(',');
                grid.loading("操作中，请稍后......");
                $.ajax({
                    url: ajaxUrl + "/modular/deleteModular.do?modularId=" + id,
                    type:"post",
                    success: function (data) {
                    	if(data.state == "eroor"){
                    		grid.unmask();
                    		mini.alert(data.message);
                    	}else{
							httpRequest({},0,pager.getPageSize());
                    	}
                    },
                    error: function () {
                        mini.alert('失败');
                    }
                });
            }
        })
    }else {
        mini.alert("请选中一条记录");
    }
}
//顺序调整：向上一行
function topChange(){
	//获得当前选中的行
	var topRow = grid.getSelected();
	var selectedId = topRow.modularId;
	if(topRow){
		var modularIdVal = topRow.modularId
		$.ajax({
			type    : 'post',
			url     : ajaxUrl + "/modular/moveUpward.do",
			data    : {
				modularId : modularIdVal,
			},
			success : function(data){
				console.log(data);
				if(data.state == 'success'){
					httpRequest(topRow,0,pager.getPageSize());
				}else{
					mini.alert(data.message);
				}
			},
			error   : function(message){
				console.log('请求失败');
			}
		})
	}else{
		mini.alert('请选择一行');
	}
}
//顺序调整：向下一行
function bottomChange(){
	//获得当前选中的行
	var bottomRow = grid.getSelected();
	if(bottomRow){
		var modularIdVal = bottomRow.modularId
		$.ajax({
			type : 'post',
			url  : ajaxUrl + '/modular/moveDown.do',
			data : {
				modularId : modularIdVal,
			},
			success : function (data){
				console.log(data);
				if(data.state=='success'){
					httpRequest(bottomRow,0,pager.getPageSize());
				}else{
					mini.alert(data.message);
				}
			},
			error : function(message){
				
			}
		})
	}
}
//查询
function search() {
    var key = mini.get("key").getValue();
    $.ajax({
        url: ajaxUrl + "/DepartmentController/getListByParam.do",
        type:"post",
        datatype:"json",
        data:{
            departmentName:key
        },
        success: function (data) {
            grid.setData(data.data)
			pager.set({"total": data.total})
			//grid.reload();
        },
        error: function () {
            mini.alert("失败");
        }
    });
}
function onKeyEnter(e) {
    search();
}

//刷新
function refresh(){
	httpRequest();
}
//数据请求
//测试svn代码提交
function httpRequest(Row,PageIndex, PageSize){
	var pageObj = {
		PageIndex: PageIndex,
		PageSize: PageSize
	}

	var pageStr = JSON.stringify(pageObj);
	$.ajax({
		url: ajaxUrl + "/modular/list.do",
        type:"post",
		data:pageStr,
		 contentType: 'application/json',
        success: function (data) {
        	console.log(data);
        	var modularData = data.data;
        	for(var i = 0; i < modularData.length; i++){
        		//日期格式化
        		if(modularData[i].createDate){
        			modularData[i].createDate = formatDate(modularData[i].createDate);
        		}else{
        			modularData[i].createDate = '';
        		}
        		//是否系统预制
        		modularData[i].modularIsPreset = modularData[i].modularIsPreset == 1 ? '是' : '否';
        		//模块状态
        		if(modularData[i].modularState){
        			modularData[i].modularState = modularData[i].modularState == 1 ? '开启' : '关闭';
        		}else{
        			modularData[i].modularState = '';
        		}
        	}
        	grid.setData(modularData);
			pager.set({"totalCount": data.total})
        	if(Row){
        		grid.select(Row,true);

        	}else{
        		
        	}
        },
        error:function(data){
            mini.alert(data.message);
        }
    });
}

//上移
function moveUp() {
    var row = grid.getSelected();
    if (row) {
        var index = grid.indexOf(row);
        grid.moveRow(row, index - 1);
    }
}
//下移
function moveDown() {
    var row = grid.getSelected();
    if (row) {
        var index = grid.indexOf(row);
        
        grid.moveRow(row, index + 2);
    }
}

//分页
grid.on("beforeload", function (e) {
	//阻止本来的翻页功能
	e.cancel = true;
	//当前页码
	var cunrrentPageIndex = grid.getPageIndex();
	//点击按钮之后的页码
	var pageIndex = e.data.pageIndex;
	//设置当前页数值
	pager.set({"pageIndex": pageIndex});
	//数据总数
	//var pageCount = pubPageCount;
	pager.set({"pageIndex": pageIndex});
	debugger
	httpRequest({},pageIndex + 1, pager.getPageSize());
});

//刷新
function onRefresh(e) {
	httpRequest({},pager.getPageIndex(), pager.getPageSize());
}
